package com.lijie.ljblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.lijie.ljblog.model.Classify;
import com.lijie.ljblog.mapper.ClassifyMapper;
import com.lijie.ljblog.service.ClassifyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lijie.ljblog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lijie
 * @since 2021-08-28
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Autowired
    ClassifyMapper classifyMapper;
    @Override
    public List<Classify> getPClassify() {
        QueryWrapper<Classify> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parentId",-1);
        List<Classify> classifies = classifyMapper.selectList(queryWrapper);
        return classifies;
    }

    /**
     *
     * @param id 分类名的id, 查询条件：parentId=id
     * @return
     */
    @Override
    public PageInfo<Classify> getClassifyByPid(Integer id) {
        List<Classify> classifyByPid = classifyMapper.getClassifyByPid(id);
        return new PageInfo<>(classifyByPid);
    }

    @Override
    public Result<Object> addClassify(Classify classify) {
        Result<Object> result=Result.getInstanceError();
        if(classify.getParentId()!=null&&classify.getValue()!=null){
            int insert = classifyMapper.insert(classify);
            if(insert==1){
                result.setSuccess();
                result.setMessage("插入成功");
            }else{
                result.setMessage("插入失败");
            }
        }
        return result;
    }
}
